import path from 'path';
import { defineConfig } from 'umi';
import externals from './externals';
import routes from '../src/config/routes';
import { APP_PATH, PUBLIC_PATH } from './constants';

export default defineConfig({
  title: '风行管理系统',
  define: {
    APP_PATH,
    'process.env': process.env,
  },
  publicPath: PUBLIC_PATH,
  hash: true,
  antd: {
    dark: false,
    compact: false,
  },
  nodeModulesTransform: {
    type: 'none',
  },
  routes: [{ path: '/', component: '@/site', routes }],
  alias: {
    '@': path.resolve(__dirname, '../src'),
    pages: path.resolve(__dirname, '../src/pages'),
    utils: path.resolve(__dirname, '../src/utils'),
    assets: path.resolve(__dirname, '../src/assets'),
    styles: path.resolve(__dirname, '../src/styles'),
  },
  chainWebpack(config) {
    config.module.rules.store.delete('eslint');
    config.optimization.splitChunks({
      cacheGroups: {
        styles: {
          name: 'styles',
          test: /\.(css|less)$/,
          chunks: 'async',
          minChunks: 1,
          minSize: 0,
        },
      },
    });

    // 添加全局less变量
    config.module
      .rule('less-global')
      .test(/\.less$/)
      .use('style-resources')
      .loader('style-resources-loader')
      .options({
        patterns: [path.resolve(__dirname, '../src/styles/constants/const.less')],
      });

    // 提出splitChunks中的公共代码
    config.merge({
      optimization: {
        splitChunks: {
          chunks: 'all',
          minSize: 30000,
          minChunks: 3,
          automaticNameDelimiter: '.',
          cacheGroups: {
            vendor: {
              name: 'vendors',
              test({ resource }) {
                return /[\\/]node_modules[\\/]/.test(resource);
              },
              priority: 10,
            },
          },
        },
      },
    });
  },
  ...externals,
});
